﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProgramMain.Database;

namespace ProgramMain.OpticNet
{
    public partial class EquipmentDetail : ProgramMain.Framework.BaseModalDetailCtl
    {
        public EquipmentDetail()
        {
            InitializeComponent();
        }

        private int? _vertexID = null;
        public int? VertexID
        {
            get { return _vertexID; }
            set { _vertexID = value; }
        }
        protected override void InitializeDataControls()
        {
            int? id = null;
            SimpleOpticNetDB.EquipmentRow equip = (SimpleOpticNetDB.EquipmentRow)((DataRowView)dataSource.Current).Row;
            if (equip != null && equip.KvadratEquipmentID > 0) id = equip.KvadratEquipmentID;

            this.vTechEquipmentsTableAdapter.SelectCommandTimeout = ProgramMain.Properties.Settings.Default.LongQueryTimeout;
            this.vTechEquipmentsTableAdapter.Fill(this.simpleOpticNetDB.vTechEquipments, true, id, VertexID);
            if (EquipBase.Count > 0)
            {
                EquipBase.MoveFirst();
                SimpleOpticNetDB.vTechEquipmentsRow techEquipmentsRow = (SimpleOpticNetDB.vTechEquipmentsRow)((DataRowView)EquipBase.Current).Row;
              if(techEquipmentsRow != null)
                lookupCurveElementTypeID.EditValue = techEquipmentsRow.ID;
                if (ShowMode == ShowModeTypes.Add)
                {
                    SimpleOpticNetDB.EquipmentRow row = (SimpleOpticNetDB.EquipmentRow) CurrentRowView.Row;
                    if (this.EquipBase.Count > 0)
                    {
                        this.EquipBase.MoveFirst();
                        row.KvadratEquipmentID = (int) lookupCurveElementTypeID.EditValue;
                        txtEditCaption.Text = lookupCurveElementTypeID.Text;
                    }
                }
            }
        }

        protected override void BindDataControls()
        {
         //   if (ShowMode != ShowModeTypes.Add)
            this.txtEditCaption.DataBindings.Add(new Binding("Text", this.dataSource, "Caption"));
            if (ShowMode == ShowModeTypes.Add)
            this.lookupCurveElementTypeID.DataBindings.Add(new Binding("EditValue", this.dataSource, "ID"));
            else
            this.lookupCurveElementTypeID.DataBindings.Add(new Binding("EditValue", this.dataSource, "KvadratEquipmentID"));
          
            this.memoEditDesc.DataBindings.Add(new Binding("Text", this.dataSource, "Description"));
            this.lookupCurveElementTypeID.Enabled = ShowMode == ShowModeTypes.Add;

        }

        public override bool CheckValidation()
        {
            return !string.IsNullOrEmpty(txtEditCaption.Text) && lookupCurveElementTypeID.EditValue != null;
        }

        private void lookupCurveElementTypeID_EditValueChanged(object sender, EventArgs e)
        {
            if (ShowMode == ShowModeTypes.Add && lookupCurveElementTypeID.EditValue != null)
            try
            {
                SimpleOpticNetDB.EquipmentRow row = (SimpleOpticNetDB.EquipmentRow) CurrentRowView.Row;
                row.KvadratEquipmentID = (int) lookupCurveElementTypeID.EditValue;
                txtEditCaption.Text = lookupCurveElementTypeID.Text;
            }
            catch
            {}
        }

        public override void btnSave_Click(object sender, EventArgs e)
        {

            if (CheckValidation())
                base.btnSave_Click(sender, e);
           
        }
    }
}
